package com.hspedu.object_;

import java.util.Objects;

public class EqualsExercise {
    public static void main(String[] args) {
        Person person1 = new Person("jack", 10, '男');
        Person person2 = new Person("jack", 10, '男');

        System.out.println(person1.equals(person2));//false
    }
}

class Person {//extends object 中的equals
    private String name;
    private int age;
    private char gender;

    //重写equals方法


    @Override
    public boolean equals(Object o) {
        //如果比较的对象是同一个对象，则直接返回true
        if (this == o) {
            return true;
        }
       /* 自己写的
       else if (o == null) {
            return false;
        } else if (o instanceof Person) {
            Person p = (Person) o;
            return p.getAge() == this.age && p.getName().equals(this.name)
                    && p.getGender() == this.gender;
        }else {
            return false;
        }*/

        //老师的思路
        if (o instanceof Person) {
            Person p = (Person) o;
            return p.getAge() == this.age && p.getName().equals(this.name)
                    && p.getGender() == this.gender;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, gender);
    }

    public Person(String name, int age, char gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }
}